from core.basehandler import BaseHandler
from core.room import Room
from core.action import Action
from items.wood import Wood
from items.grass import Grass
from items.dumbbells import Dumbbells
from items.rope import Rope
from core.support import set_cutscene
from core.support import get_game
from rooms.victory_room import VictoryRoom


class PrisonYard(Room):
    def __init__(self, room_id):
        Room.__init__(self, room_id)    # do basic initialization for every room
        
        # add inventory items
        self.add_item(Wood())
        self.add_item(Grass("grass1"))
        self.add_item(Grass("grass2"))
        self.add_item(Grass("grass3"))
        self.add_item(Dumbbells())
                
        # build list of actions
        self.add_action(GoBackInside)
        self.add_action(GoOverFence)
        self.add_action(WeaveRope)
        self.add_action(PrisonRiot)
    # return description of room.
    def get_description(self):
        return "MacGyver is standing in the prison yard.  A 20-foot fence separates MacGyver from freedom.  A handful of inmates are gathered in the field."
    
    # return image of room
    def get_image(self):
        return 'prison_yard.jpg'

class PrisonYardHandler(BaseHandler):
    
    # return class to use to create a new instance of this room
    def get_room_class(self):
        return PrisonYard

# define actions for this room

class PrisonRiot(Action):
    def __init__(self):
        Action.__init__(self, "prison_riot")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Start Prison Riot"
    
    def do_action(self,game,room,request):
        game.get_room('victory_room',VictoryRoom).set_image('PrisonRiot.jpg')
        game.get_room('victory_room',VictoryRoom).set_description('MacGyver started a riot and was killed in the madness.')
        return
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'victory_room'
    
    def is_enabled(self):
        return get_game().get_player().has_item('wood')
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "get"

class WeaveRope(Action):
    def __init__(self):
        Action.__init__(self, "weave_rope")
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Weave Rope"
    
    def do_action(self,game,room,request):
        set_cutscene("MacGyver weaves a durable rope")
        game.get_player().pop_item('grass1')
        game.get_player().pop_item('grass2')
        game.get_player().pop_item('grass3')
        game.get_player().add_item(Rope())
        return
    
    def get_method(self):
        return "get"
    
    def is_enabled(self):
        game = get_game()
        player = game.get_player()
        return (player.has_item('grass1') and player.has_item('grass2') and player.has_item('grass3'))

class GoBackInside(Action):
    def __init__(self):
        Action.__init__(self, "GoBackInside")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Go Back Inside"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'prisonhall'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "get"

class GoOverFence(Action):
    def __init__(self):
        Action.__init__(self, "GoOverFence")
   
    def get_description(self):
        return "Go Over Fence"
    
    def get_destination(self):
        return 'beachroom'
     
    def get_method(self):        
        return "get"
    
    def is_enabled(self):
        return get_game().get_player().has_item('rope')
